package com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIllegal.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIllegal.biz.service.EvaluationOrgIllegalService;
import com.paic.mhis.hcpms.evaluation.intergration.dto.FwaOrgDTO;
import com.paic.mhis.hcpms.evaluation.intergration.sao.FwaOrgSao;
import com.paic.pafa.app.integration.sa.PafaSAOException;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/evaluationOrgIllegal/")
public class EvaluationOrgIllegalController {

	@BizServicesProxy
	private EvaluationOrgIllegalService evaluationOrgIllegalService;
	private Log log = LogFactory.getLog(this.getClass());
	
	@BizServicesProxy
	private FwaOrgSao fwaOrgSao;

	@RequestMapping("queryEvaluationOrgIllegal.do")
	@ResponseBody
	public Map<String, Object> queryEvaluationOrgIllegal(@ModelAttribute EvaluationOrgIllegalVO vo,
			HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		// 开始日期
		if (null != vo && StringUtils.isNotBlank(vo.getStartDate()) && StringUtils.isNotEmpty(vo.getStartDate())) {
			map.put("startDate", vo.getStartDate());
		}
		// 结束日期
		if (null != vo && StringUtils.isNotBlank(vo.getEndDate()) && StringUtils.isNotEmpty(vo.getEndDate())) {
			map.put("endDate", vo.getEndDate());
		}
		// 机构名称
		if (null != vo && StringUtils.isNotBlank(vo.getOrgCode()) && StringUtils.isNotEmpty(vo.getOrgCode())) {
			map.put("orgCode", vo.getOrgCode());
		}
		result = evaluationOrgIllegalService.queryEvaluationOrgIllegal(map);
		
		try {
			//调FWA接口
			map1 = fwaOrgSao.invokeOrgInfoESA(vo.getOrgCode(), vo.getStartDate(), vo.getEndDate());
		} catch (PafaSAOException e) {
			e.printStackTrace();
		}
		
		result.put("map1", map1);
		return result;
	}

	@RequestMapping("queryEvaluationOrgIllegalWarnDetails.do")
	@ResponseBody
	public Map<String, Object> queryEvaluationOrgIllegalWarnDetails(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgCode", request.getParameter("orgCode"));
		// 处理方式
		map.put("process", request.getParameter("code"));
		map.put("startDate", request.getParameter("startDate"));
		map.put("endDate", request.getParameter("endDate"));
		return evaluationOrgIllegalService.queryEvaluationOrgIllegalWarnDetails(map);
	}

	@RequestMapping("queryEvaluationOrgIllegalAuditDetails.do")
	@ResponseBody
	public Map<String, Object> queryEvaluationOrgIllegalAuditDetails(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgCode", request.getParameter("orgCode"));
		map.put("startDate", request.getParameter("startDate"));
		map.put("endDate", request.getParameter("endDate"));
		return evaluationOrgIllegalService.queryEvaluationOrgIllegalAuditDetails(map);
	}
	
		//queryEvaluationOrgIllegalPersonDetails.do
		@RequestMapping("queryEvaluationOrgIllegalPersonDetails.do")
		@ResponseBody
		public Map<String, Object> queryEvaluationOrgIllegalPersonDetails(HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orgCode", request.getParameter("orgCode"));
			map.put("startDate",request.getParameter("startDate"));
			map.put("endDate",request.getParameter("endDate"));
			map.put("cut12", request.getParameter("code"));
			return evaluationOrgIllegalService.queryEvaluationOrgIllegalPersonDetails(map);
		}
	
	@RequestMapping("exportOrgIllegalToCsv.do")
	@ResponseBody
	public void exportOrgIllegalToCsv(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map.put("orgCode", request.getParameter("orgCode"));
		map.put("startDate", request.getParameter("startDate"));
		map.put("endDate", request.getParameter("endDate"));
		map.put("orgName", request.getParameter("orgName"));
		String name = map.get("orgName").toString() + "违规情况";
		
		try {
			//调FWA接口
			map1 = fwaOrgSao.invokeOrgInfoESA(request.getParameter("orgCode"), request.getParameter("startDate"), request.getParameter("endDate"));
		} catch (PafaSAOException e) {
			e.printStackTrace();
		}
		
		try {
			exportExcel(map, name, response,map1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void exportExcel(Map<String, Object> map, String excelName, HttpServletResponse response,Map<String, Object> map1)
			throws IOException {
		Map<String, Object> warnResultMap = evaluationOrgIllegalService.queryEvaluationOrgIllegal(map);
		@SuppressWarnings("unchecked")
		Map<String, Object> warnData = (Map<String, Object>) warnResultMap.get("warnData");
		@SuppressWarnings("unchecked")
		Map<String, Object> auditData = (Map<String, Object>) warnResultMap.get("auditData");
		String suspiciousNum = "0";//可疑条数
		String suspiciousAmount = "0";//可疑金额
		String highlySuspiciousNum = "0";//高度可疑条数 
		String highlySuspiciousAmount = "0";//高度可疑金额 
		String outOfLineNum = "0";//违规条数     
		String outOfLineAmount = "0";//违规金额     
		String numTotal = map1.get("numTotal")+"";//单据数
		String amountTotal = map1.get("amountTotal")+"";//单据涉及金额
		List<FwaOrgDTO> Fwalist  = (List<FwaOrgDTO>) map1.get("list");
		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			CellStyle style = excel.createCellStyle();

			style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 创建一个居中格式
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			row.createCell(j).setCellValue(map.get("orgName") + "违规情况:");
			sheet.setColumnWidth(j, 5000);
			for(int k=1;k<10;k++)
			{
			sheet.setColumnWidth(k, 5000);
			}
			// 设置Excel内容
			String warnCount = warnData.get("num") == null ? "0" : warnData.get("num").toString();
			row = sheet.createRow(2);
			row.createCell(1).setCellValue("1、预警次数：");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(Double.parseDouble(warnCount));
			row.createCell(3).setCellValue("次");
			String amount = warnData.get("money") == null ? "0" : warnData.get("money").toString();
			row.createCell(4).setCellValue("涉及金额:");
			//row.createCell(6).setCellValue(amount);//2
			row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(5).setCellValue(Double.parseDouble(amount));
			row.createCell(6).setCellValue("元");
			List<Map<String, Object>> list = (List<Map<String, Object>>) warnData.get("list");
			for (int i = 0; i < list.size(); i++) {
				HashMap<String, Object> tempMap = (HashMap<String, Object>) list.get(i);
				String tempName = (String) tempMap.get("name");
				String tempCount = tempMap.get("num") == null ? "0" : tempMap.get("num").toString();
				String tempMoney = tempMap.get("money") == null ? "0" : tempMap.get("money").toString();
				row = sheet.createRow(3 + i);
				if (i == 0) {
					row.createCell(0).setCellValue("其中:");
				}
				row.createCell(1).setCellValue(tempName + ":");
				row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				row.getCell(2).setCellValue(Double.parseDouble(tempCount));
				row.createCell(3).setCellValue("次");
				row.createCell(4).setCellValue("涉及金额:");
				row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				row.getCell(5).setCellValue(Double.parseDouble(tempMoney));
				row.createCell(6).setCellValue("元");

			}
			
			for(FwaOrgDTO fList : Fwalist){
				 if(fList.getName().equals("可疑")){
					 suspiciousNum = fList.getNum()+"";
					 suspiciousAmount = fList.getMoney()+"";
				 }
				 if(fList.getName().equals("高度可疑")){
					 highlySuspiciousNum = fList.getNum()+"";
					 highlySuspiciousAmount = fList.getMoney()+"";
				 }
				 if(fList.getName().equals("违规")){
					 outOfLineNum = fList.getNum()+"";
					 outOfLineAmount = fList.getMoney()+"";
				 }
			}
			
			String auditCount = auditData.get("num") == null ? "0" : auditData.get("num").toString();
			String auditamount = auditData.get("money") == null ? "0" : auditData.get("money").toString();
			row = sheet.createRow(9);
			row.createCell(1).setCellValue("2、稽核次数:");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(Double.parseDouble(auditCount));
			row.createCell(3).setCellValue("次");
			row.createCell(4).setCellValue("涉及金额:");
			
			row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(5).setCellValue(Double.parseDouble(auditamount));
			row.createCell(6).setCellValue("元");

			row = sheet.createRow(11);
			row.createCell(1).setCellValue("3、其中一次记十二分:");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(0);
			row.createCell(3).setCellValue("次");

			row = sheet.createRow(13);
			row.createCell(0).setCellValue("4、FWA事后审核情况:");

			row = sheet.createRow(14);

			row.createCell(1).setCellValue("单据数:");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(numTotal);
			row.createCell(3).setCellValue("条");
			row.createCell(4).setCellValue("涉及金额:");
			row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(5).setCellValue(amountTotal);
			row.createCell(6).setCellValue("元");

			row = sheet.createRow(15);
			row.createCell(0).setCellValue("其中:");
			row.createCell(1).setCellValue("可疑:");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(suspiciousNum);
			row.createCell(3).setCellValue("条");
			row.createCell(4).setCellValue("涉及金额:");
			row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(5).setCellValue(suspiciousAmount);
			row.createCell(6).setCellValue("元");

			row = sheet.createRow(16);
			row.createCell(1).setCellValue("高度可疑:");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(highlySuspiciousNum);
			row.createCell(3).setCellValue("条");
			row.createCell(4).setCellValue("涉及金额:");
			row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(5).setCellValue(highlySuspiciousAmount);
			row.createCell(6).setCellValue("元");

			row = sheet.createRow(17);
			row.createCell(1).setCellValue("违规:");
			row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(2).setCellValue(outOfLineNum);
			row.createCell(3).setCellValue("条");
			row.createCell(4).setCellValue("涉及金额:");
			row.createCell(5).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			row.getCell(5).setCellValue(outOfLineAmount);
			row.createCell(6).setCellValue("元");

			// Map dto = (Map) resultList.get(i);
			// row = sheet.createRow(i + 1);
			// row.createCell(0).setCellValue((String) dto.get("name"));

			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			log.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();

		}

	}

	private void initResponse(HttpServletResponse response, String excelName) {
		try {
			response.addHeader("Pragma", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
			response.reset();
			response.setContentType("APPLICATION/OCTET-STREAM");
			String fileName = new String((excelName).getBytes("utf-8"), "ISO8859-1") + ".xls";
			response.setHeader("content-disposition", "attachment;fileName=\"" + fileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
}
